package com.mengtian_tech.smart_lab.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jeecgframework.core.util.ResourceUtil;

import com.mengtian_tech.smart_lab.entity.system.TSUser;

public class LoginFilter implements Filter {

	private List<String> urlList = new ArrayList<>();
	private Log log = LogFactory.getLog(getClass());
    /**
     * Default constructor. 
     */
    public LoginFilter() {
    	urlList.add("loginController");
    	urlList.add("cms-attach");
    	urlList.add("cms-attach");
    	urlList.add("login2");
    	urlList.add("registerInfoController");
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}
	
	private boolean isURLExclude(String param){
		for(String url : urlList){
			if(param.indexOf(url) != -1){
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		try{
			doInnerFilter(request, response, chain);
		}catch (Exception e) {
			log.error(e.getMessage(),e);
			redirectTologin((HttpServletRequest)request,(HttpServletResponse) response);
		}		
	}

	private void redirectTologin(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Enumeration<String> enumeration =  req.getParameterNames();
			 		
			req.getSession().setAttribute("functionId", req.getParameter("clickFunctionId"));			
			System.out.println("functionId="+req.getParameter("clickFunctionId"));
			resp.sendRedirect("http://"+req.getServerName()+"/lab/loginController.do?login");
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	private void doInnerFilter(ServletRequest request,
			ServletResponse response, FilterChain chain) throws Exception
			{
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)response;
		
		String uri = req.getRequestURI();
		TSUser user = (TSUser)req.getSession().getAttribute(ResourceUtil.LOCAL_CLINET_USER);
		
		if(isURLExclude(uri) ||
				req.getParameter("changepassword") != null ||
				req.getParameter("savenewpwd") != null){
			chain.doFilter(request, response);
		}else{
			if(user == null){
				String loginURL = "http://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+"/loginController.do?login";
				resp.sendRedirect(loginURL);
			}else{
				chain.doFilter(request, response);
			}
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {		
	}
}